﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Comm;
using VehicleMaintenance.Models;
using VehicleMaintenance.BaseInfo;

namespace VehicleMaintenance.Transaction
{
	public partial class MainBillForm : AddOrEditBaseForm
	{

		#region 私有变量及属性

		/// <summary>
		/// 记录当前ID
		/// </summary>
		private int billID = 0;

		/// <summary>
		/// 当输入车牌号时判断该车是否登记过
		/// </summary>
		private bool isNewCar = false;

		#endregion 私有变量及属性

		public MainBillForm()
		{
			InitializeComponent();
			ucFormInput1.InitializeControls(typeof(Models.MainBill));
			InitializeSepcial();
		}

		public MainBillForm(int id)
		{
			InitializeComponent();
			ucFormInput1.InitializeControls(typeof(Models.MainBill));
			InitializeSepcial();

			billID = id;
			state = State.Edit;
		}

		private void CarInfoForm_Load(object sender, EventArgs e)
		{
			try
			{
				if (this.state == State.Edit)
				{
					CarDBContext db = new CarDBContext();

					MainBill bill = db.MainBill.Include("CarInfo").Include("MaintenanceCategories").First(p => p.ID == this.billID);
					ucFormInput1.SetValues(bill);

					ucFormInput1.GetControlByName("CarID").Text = bill.Car.CarName;
					ucFormInput1.GetControlByName("CarNO").Text = bill.Car.NO;

					ucFormInput1.GetControlByName("CategoryID").Text = bill.MaintenanceCategories.Name;
					ucFormInput1.GetControlByName("CategoryID").Tag = bill.MaintenanceCategories.CategoryID;
				}
				else
				{
					ucFormInput1.ClearData();

					InitializeDefault();
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			try
			{
				CarDBContext db = new CarDBContext();

				if (!ucFormInput1.VerifyInput())
				{
					return;
				}

				if (this.state == State.Edit)
				{
					MainBill bill = db.MainBill.First(p => p.ID == billID);
					ucFormInput1.GetValues(bill);
					db.SaveChanges();

					this.ShowInfo("保存成功！");
				}
				else
				{
					var bill = db.MainBill.Create();

					ucFormInput1.GetValues(bill);
					db.MainBill.Add(bill);
					db.SaveChanges();

					if (MessageBox.Show(this, "保存成功，是否继续添加？", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
					{
						ucFormInput1.ClearData();
						InitializeDefault();
						ucFormInput1.GetControlByName("MainBillCode").Focus();
					}
					else
					{
						this.DialogResult = System.Windows.Forms.DialogResult.OK;
					}
				}

			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			try
			{
				this.Close();
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void InitializeSepcial()
		{
			try
			{
				ucFormInput1.GetControlByName("CarID").Click += new EventHandler(car_Click);
				ucFormInput1.GetControlByName("CategoryID").Click += new EventHandler(Category_Click);
				ucFormInput1.GetControlByName("CarNO").TextChanged += new EventHandler(MainBillForm_TextChanged);
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		void MainBillForm_TextChanged(object sender, EventArgs e)
		{
			try
			{
				TextBox txt = (sender as TextBox);
				if (!string.IsNullOrEmpty(txt.Text.Trim()))
				{
					CarDBContext db = new CarDBContext();
					CarInfo car = db.CarInfo.FirstOrDefault(p => p.NO == txt.Text.Trim());
					if (car != null)
					{
						ucFormInput1.GetControlByName("CarID").Text = car.CarName;
						ucFormInput1.GetControlByName("CarID").Tag = car.CarID;

						isNewCar = false;
					}
					else
					{
						isNewCar = true;
					}
				}
				else
				{
					isNewCar = false;
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}


		void car_Click(object sender, EventArgs e)
		{
			try
			{
				TextBox txt = (sender as TextBox);

				if (isNewCar)
				{
					CarInfoForm form = new CarInfoForm(ucFormInput1.GetControlByName("CarNO").Text);
					if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
					{
						txt.Text = form.Car.CarName;
						txt.Tag = form.Car.CarID;
						isNewCar = false;
					}

				}
				else
				{
					CarListForm form = new CarListForm();
					if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
					{
						txt.Text = form.Car.CarName;
						txt.Tag = form.Car.CarID;

						ucFormInput1.GetControlByName("CarNO").Text = form.Car.NO;
					}
				}

			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		void Category_Click(object sender, EventArgs e)
		{
			try
			{
				TextBox txt = (sender as TextBox);

				MaintenanceCategoriesForm form = new MaintenanceCategoriesForm(true);
				if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
				{
					txt.Text = form.Category.Name;
					txt.Tag = form.Category.CategoryID;
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		void InitializeDefault()
		{
			CarDBContext db = new CarDBContext();
			string str = DateTime.Now.ToString("yyyyMMdd");
			MainBill mainBill = db.MainBill.OrderByDescending(p => p.ID).FirstOrDefault(p => p.MainBillCode.StartsWith(str));
			if (mainBill == null)
			{
				str += "001";
			}
			else
			{
				str = (Convert.ToInt64(mainBill.MainBillCode) + 1).ToString();
			}
			ucFormInput1.GetControlByName("MainBillCode").Text = str;
			ucFormInput1.GetControlByName("State").Text = "已登记";
		}
	}
}
